//
// Created by lyx on 2020/4/4.
//
#include <iostream>
#include <Utils.h>
//namespace xos=lyxutils::os;
//namespace xio=lyxutils::io;
//namespace str=lyxutils::str_utils;
//namespace np=lyxutils::numpy;
using namespace std;
using namespace lyxutils::abbreviations;

int main(int arc,char **argv){
    //set working directory in this folder when test
    int width=80;
    cout<<str::center("1.test file/directory existence",width,'*')<<endl;
    vector<string> files={"exist.txt","non-exist.txt","/home/lyx/workspace/clion/Utils/test/exist.txt",
                          "/home/lyx/workspace/clion/Utils/test/non-exist.txt","/home/lyx/workspace/",
                          "/home/zhy/workspace/","../cmake_modules/","../non_exist_folder/"};
    for(auto f:files){
        cout << "exist:" << f << ",exist?" << xos::exist(f) << ",is a regular file?"<<xos::file(f)<<",is a directory?" << xos::directory(f) << endl;
    }
    cout<<str::center("2.test directory operations",width,'*')<<endl;
    string current=xos::pwd();
    string pf=xos::parent(current);
    string tf="../cmake_modules/readme.md";
    string tfupup="../../../../../../test/build";
    cout<<"test file:"<<tf<<",absolute path name:"<<xos::abspath(tf)<<",parent:"<<xos::parent(tf)<<endl;
    cout<<"test file2:"<<tfupup<<",absolute path name:"<<xos::abspath(tfupup)<<",parent:"<<xos::parent(tfupup)<<endl;
    cout<<"base name of test exist:"<<xos::basename(tf)<<",suffix:"<<xos::suffix(tf)<<endl;
    cout << "fullname(test file)=>(full name):" << xos::fullname(tf) << ",fullname(fullname(test file))=" << xos::fullname(xos::fullname(tf)) << endl;
    cout<<"current directory:"<<current<<",parent:"<<pf<<endl;
    cout<<endl<<"all children of folder:"<<xos::parent(xos::pwd())<<endl;
    std::vector<std::string> dirs=xos::ls(xos::parent(xos::pwd()));
    std::vector<std::string> dirs_new;
    np::apply(dirs,dirs_new,std::bind(xos::relpath,std::placeholders::_1,xos::pwd()));
    cout<<"==in absolute name(default)=="<<endl;
    xio::printVector(dirs,"\n");
    cout<<"==in relative name=="<<endl;
    xio::printVector(dirs_new,"\n");
    cout<<"current:"<<xos::pwd()<<endl;
    cout<<"relative path from current folder to "<<tf<<":"<<xos::relpath(tf)<<endl;
    string tf1="/home/lyx/workspace/vscode/test/";
    cout<<"relative path from current to "<<tf1<<":"<<xos::relpath(tf1)<<endl;
    cout<<str::center("3.move around",width,'*')<<endl;
    cout<<"current folder:"<<xos::pwd()<<endl;
    xos::cd("..");
    cout<<"change up directory,current:"<<xos::pwd()<<endl;
    string no_folder="/home/non_folder/";
    bool success=xos::cd(no_folder);
    cout<<"change to non-exist folder:"<<no_folder<<"=>"<<(success?"succeed":"failed")<<",current:"<<xos::pwd()<<endl;
    cout<<"change to /opt=>"<<(xos::cd("/opt")?"succeed":"failed")<<",current:"<<xos::pwd()<<endl;
    cout<<"change to ./go/bin=>"<<(xos::cd("./go/bin")?"succeed":"failed")<<",current:"<<xos::pwd()<<endl;
    cout<<str::center("4.create and remove files test",width,'*')<<endl;
    string cf1="./folder1";
    string cf2="./folder2/folder_again";
    try{
        xos::cd(current);
        cout<<"current:"<<xos::pwd()<<endl;
        cout<<"create folder:"<<cf1<<"=>"<<(xos::mkdir(cf1)?"success":"failed")<<endl;
        cout<<"create folder:"<<cf2<<"=>"<<(xos::mkdir(cf2,true)?"success":"failed")<<endl;
    }catch(exception e){
        cout<<e.what()<<endl;
    }
    return 0;
}